<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('会员列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>会员搜索：</label>
                            <input type="text" name="memberName" placeholder="请输入会员姓名或手机号"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <!--<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="business:member:add">
                <i class="fa fa-plus"></i> 添加
            </a>-->
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="business:member:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="business:member:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="business:member:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('business:member:edit')}]];
    var removeFlag = [[${@permission.hasPermi('business:member:remove')}]];
    var prefix = ctx + "business/member";

    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "会员",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '会员ID',
                    visible: false
                },
                {
                    field: 'memberName',
                    title: '会员姓名'
                },
                {
                    field: 'mobile',
                    title: '会员手机号码'
                },
                {
                    field: 'recommendName',
                    title: '推荐人姓名'
                },
                {
                    field: 'recommendMobile',
                    title: '推荐人手机号码'
                },
                {
                    field: 'createTime',
                    title: '注册时间'
                },
                {
                    field: 'douBalance',
                    align: 'center',
                    title: '福豆余额',
                    formatter: function(value, row, index) {
                        return  "<a href='javascript:void(0)' onclick='showDetail(" + row.id + ", 0)'>" + value + "</a>";
                    }
                },
                {
                    field: 'douPerson',
                    align: 'center',
                    title: '个人福豆',
                    formatter: function(value, row, index) {
                        return  "<a href='javascript:void(0)' onclick='showDetail(" + row.id + ", 1)'>" + value + "</a>";
                    }
                },
                {
                    field: 'douTeam',
                    align: 'center',
                    title: '团队福豆',
                    formatter: function(value, row, index) {
                        return  "<a href='javascript:void(0)' onclick='showDetail(" + row.id + ", 2)'>" + value + "</a>";
                    }
                },
                {
                    field: 'douField',
                    align: 'center',
                    title: '福豆田',
                    formatter: function(value, row, index) {
                        return  "<a href='javascript:void(0)' onclick='showDetail(" + row.id + ", 4)'>" + value + "</a>";
                    }
                },
                {
                    field: 'douSpecial',
                    align: 'center',
                    title: '专项福豆',
                    formatter: function(value, row, index) {
                        return  "<a href='javascript:void(0)' onclick='showDetail(" + row.id + ", 3)'>" + value + "</a>";
                    }
                },
                {
                    field: 'selfNum',
                    align: 'center',
                    title: '持仓盒数'
                },
                {
                    field: 'memberType',
                    title: '会员类型',
                    formatter: function(value, row, index) {
                        return  value ? "个人用户" : "经销商";
                    }
                },
                {
                    field: 'isEnable',
                    title: '是否有效',
                    formatter: function(value, row, index) {
                        if(value){
                            return "<span style='color:#00AA00'>有效</span>";
                        }else{
                            return "<span style='color:#AA0000'>失效</span>";
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="showTeam(\'' + row.id + '\', \'' + row.memberName + row.mobile + '\')"><i class="fa fa-share-alt"></i>会员架构</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="updatePwd(\'' + row.id + '\', \'' + row.memberName + '\')"><i class="fa fa-cog"></i>修改密码</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="updateRecommend(\'' + row.id + '\', \'' + row.memberName + row.mobile  + '\')"><i class="fa fa-cog"></i>修改推荐人</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    //查看明细
    function showDetail(memberID, accountType){
        location.href = prefix + "/accountDetail?memberID=" + memberID + "&accountType=" + accountType;
    }

    //会员架构
    function showTeam(memberID, title){
        $.modal.open(title + " 会员架构", prefix + "/accountTeam?memberID=" + memberID);
    }

    //修改密码
    function updatePwd(memberID, memberName){
        $.post(prefix + "/showPassword", {
            memberID:memberID
        }, function(response){
            if(response.code == 0){
                showPwd(response.data.pwd)
            }
        });

        let showPwd = function(oldPwd){
            $.modal.confirm("<div id='anch'>请输入新的密码&nbsp;&nbsp;" + memberName + "<br/><input class='form-control' id='newPwd' value='" + oldPwd + "' maxlength='30'></div>", function() {
                let pwd = $("#newPwd").val();
                if(pwd.length == 0){
                    $.modal.msgError("请输入新密码");
                    return false;
                }
                $.operate.post(prefix + "/editPassword", {
                    memberID:memberID,
                    password:pwd
                }, function(response){
                    if(response.code == 0){
                        $.modal.alertSuccess('操作成功！')
                    }
                });
            });
            $("#anch").parent().css("padding", "10px 20px").children(".layui-layer-ico").hide();
        }
    }

    function updateRecommend(memberID, memberName){
        $.modal.confirm("<div id='anch'>请输入" + memberName + "新推荐人手机号&nbsp;&nbsp;<br/><input class='form-control' id='newMobile' maxlength='11'></div>", function() {
            let mobile = $("#newMobile").val();
            if(!/^\d+$/.test(mobile) || mobile.length != 11){
                $.modal.msgError("请输入正确的手机号");
                return false;
            }
            $.operate.post(prefix + "/editRecommend", {
                memberID:memberID,
                newRecommendMobile:mobile
            }, function(response){
                if(response.code == 0){
                    $.modal.alertSuccess('操作成功！')
                }
            });
        });
        $("#anch").parent().css("padding", "10px 20px").children(".layui-layer-ico").hide();
    }
</script>
</body>
</html>